#define _CRT_SECURE_NO_WARNINGS 1
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;

struct pd
{
    int dif;
    int pro;
}PD[100001];

class Solution {
public:
    int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker)
    {
        if (worker[max_element(worker.begin(), worker.end()) - worker.begin()] < difficulty[min_element(difficulty.begin(), difficulty.end()) - difficulty.begin()])
            return 0;
        for (int i = 0; i < difficulty.size(); i++)
        {
            PD[i].dif = difficulty[i];
            PD[i].pro = profit[i];
        }
        sort(PD, PD + difficulty.size(), [](pd x, pd y) { return x.dif < y.dif; });
        sort(worker.begin(), worker.end());
        int sum = 0;
        int max = 0;
        for (int i = 0; i < worker.size(); i++)
        {
            for (int j = 0; j < difficulty.size() && PD[j].dif <= worker[i]; j++)
                max = max > PD[j].pro ? max : PD[j].pro;
            sum += max;
        }
        return sum;
    }
};